package com.unidt.www.module.infra.framework.security.config;

import com.unidt.www.framework.security.config.AuthorizeRequestsCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;

/**
 * Infra 模块的 Security 配置
 */
@Configuration("infraSecurityConfiguration")
public class SecurityConfiguration {

    @Value("${spring.boot.admin.context-path:''}")
    private String adminSeverContextPath;

    @Bean("infraAuthorizeRequestsCustomizer")
    public AuthorizeRequestsCustomizer authorizeRequestsCustomizer() {
        return new AuthorizeRequestsCustomizer() {

            @Override
            public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry) {
                // Swagger 接口文档
                registry.antMatchers("/swagger-ui.html").anonymous()
                        .antMatchers("/swagger-resources/**").anonymous()
                        .antMatchers("/webjars/**").anonymous()
                        .antMatchers("/*/api-docs").anonymous();
                //积木报表
                registry.antMatchers("/jmreport/**").permitAll();
                // Spring Boot Actuator 的安全配置
                registry.antMatchers("/actuator").anonymous()
                        .antMatchers("/actuator/**").anonymous();
                // Druid 监控
                registry.antMatchers("/druid/**").anonymous();
                // Spring Boot Admin Server 的安全配置
                registry.antMatchers(adminSeverContextPath).anonymous()
                        .antMatchers(adminSeverContextPath + "/**").anonymous();

                //欧阳自己玩的拦截 todo 欧阳
                registry.antMatchers(buildAdminApi("/enterprise-do/**")).anonymous();
                registry.antMatchers(buildAdminApi("/enterprise-do/**")).permitAll();

                registry.antMatchers(buildAdminApi("/doc/api/**")).anonymous();
                registry.antMatchers(buildAdminApi("/workbar/**")).anonymous();
                registry.antMatchers(buildAdminApi("/project/apply/**")).anonymous();
                registry.antMatchers(buildAdminApi("/bpm/form/**")).permitAll();


                /**
                 * 专家确认
                 */
                registry.antMatchers(buildAdminApi("/expert/invitation_confirm/**")).anonymous();
                registry.antMatchers(buildAdminApi("/expert/invitation_confirm/**")).permitAll();

               // registry.antMatchers(buildAdminApi("/infra/file/**")).anonymous();

                registry.antMatchers(buildAdminApi("/doc/api/**")).permitAll();
                registry.antMatchers(buildAdminApi("/workbar/**")).permitAll();
                registry.antMatchers(buildAdminApi("/project/apply/**")).permitAll();
                registry.antMatchers(buildAdminApi("/infra/file/**")).permitAll();


            }

        };
    }

}
